<template lang="html">
  <div>123</div>
</template>

<script>
import _ from 'lodash'
export default {
  data() {
    return {

    }
  },
  methods: {
    init() {
      // 先创建年item
      let arr = [
        {id: 5, name: '菠萝', time: '2015-09-12 10:03:10:150000'},
        {id: 1, name: '苹果', time: '2017-05-13 10:03:10:150000'},
        {id: 2, name: '香蕉', time: '2017-05-13 10:03:10:150000'},
        {id: 3, name: '雪梨', time: '2016-05-11 10:03:10:150000'},
        {id: 4, name: '葡萄', time: '2017-05-12 10:03:10:150000'},
        {id: 5, name: '菠萝', time: '2015-09-12 10:03:10:150000'},
        {id: 6, name: '柠檬', time: '2016-05-12 10:03:10:150000'}
      ];
      let resetArr = [];
      let yearList = [];
      let result = [];
      let tmpObj = {};

      arr = _.sortBy(arr, 'time').reverse();

      for (var i in arr) {
        let time = new Date(arr[i].time.substring(0, arr[i].time.lastIndexOf(':')));
        let year = time.getFullYear();
        let month = (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1);
        let day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
        let date = month + '-' + day;
        arr[i].date = date;
        arr[i].year = year;
        yearList.push(year);
        // resetArr.push({year: year})
      }
      // 年份去重
      // yearList
      yearList.forEach(function(a) {
        var key = (typeof a) + a;
        if (!tmpObj[key]) {
          tmpObj[key] = true;
          result.push(a);
        }
      });
      yearList = result;
      // resetArr
      if (yearList.length > 0) {
        yearList.map((item) => {
          resetArr.push({year: item, list: []})
        })
      }

      if (resetArr.length > 0) {
        for (var i in arr) {
          for (var j in resetArr) {
            if (resetArr[j].year === arr[i].year) {
              resetArr[j].list.push(arr[i])
            }
          }
        }
      }

      console.log(resetArr);
    }
  },
  created() {
    this.init();
  }
}
</script>

<style lang="scss" scoped>
div {
  font-size: 1rem;
}
</style>
